<?php
	include 'commonAccountFunctions.php';
	
	// global variable for user accounts
	$useraccts = array();
	
	session_start();
	$user = checkAuthentication();
	
	if(!(getPermissions($user)=="labman")){
		header('Location: controlPanel.php');
	}
	
	print_header();
?>
	<div id="topbar">
			<div id="title"> Lab-Check</div>
			<div id="rightbutton"> <a href="index.php" >Log Out</a> </div>
	</div>
	<div id="tributton">
		<div class="links">
			<a href="controlpanel.php">Control Panel</a>
			<a id="pressed" href="manageUserAccounts.php">Manage Accounts</a>
		</div>
	</div>
	
	<div id="content">
		<ul class="pageitem" style="padding:5px">
			<li><strong>Create new user:</strong></li>
		</ul>
		<form action="createAccount.php" method="post">
			<ul class="pageitem">
				<li class="bigfield"><input name="userName" type="text" placeholder="Username" /></li>
				<li class="bigfield"><input name="password" type="password" placeholder="Password" /></li>
				<li class="bigfield"><input name="verifyPassword" type="password" placeholder="Confirm Password" /></li>
				<li class="bigfield"><input name="emailAddress" type="text" placeholder="E-mail" /></li>
				<li class="bigfield"><input name="verifyEmail" type="text" placeholder="Confirm e-mail" /></li>
				<li class="select">
					<select name="authorization">
						<option value="labins">Inspector</option>
						<option value="labaud">Auditor</option>
						<option value="labman">Lab Manager</option>
					</select>
					<span class="arrow"></span>
				</li>
				<li class="bigfield"><input type="submit" value="Create User" /></li>
			</ul>
		</form>
		
		<ul class="pageitem" style="padding:5px">
			<li><strong>List of Users:</strong></li>
<?php
			populateUseraccts();
			$test=validateUsers($useraccts);
		
			foreach($test as $element){
				$username=explode(".", $element);
				print "<li class=\"bigfield\"><strong>$username[0]</strong></li>";
			}
?>
		</ul>
		
		<ul class="pageitem" style="padding:5xp">
			<li><strong>Delete Account:</strong></li>
		</ul>
		<form action="deleteAccount.php">
			<ul class="pageitem">
				<li class="bigfield"><input type="text" name="username" placeholder="Username"></input></li>
				<li class="bigfield"><input type="submit" value="Delete User" /></li>
			</ul>
		</form>
	</div>
<?php
	print_footer();
	
	// void -> void
	// fills the useraccts global array with the txt files associated
	// with the users
	function populateUseraccts(){
		global $useraccts;
		// source: http://php.net/manual/en/function.readdir.php
		// example #1
		if ($handle = opendir('../user-accounts')) {
			/* This is the correct way to loop over the directory. */
			while (false !== ($entry = readdir($handle))) {
				array_push($useraccts, $entry);
			}
			closedir($handle);
		}
	}
	
	// array -> array
	// makes sure that the accounts in $useraccts are valid names (ie not a .)
	function validateUsers($acctArray){
		$accounts = array();
		foreach($acctArray as $element){
			if($element=="." || $element==".."){
				// do nothing
			} else{
				array_push($accounts, $element);
			}
		}
		 return $accounts;
	}
?>